
var StartLayer = cc.Layer.extend({
	ctor:function () {
		this._super();
		
		music = Storage.getMusicState();
		MusicPlayer.playStartLayerBgMusic();
		
		if (firstOpenStart) {
			firstOpenStart = false;
//			jsb.reflection.callStaticMethod
//				("org/cocos2dx/javascript/AppActivity", "autoUpdate", "()V");
		}
		
		barLevel = EnhenceSys.barWidthEnhence.get();
		switch(barLevel) {
		case 1:
			barWidth = 374 * (1 + 1/4);
			break;
		case 2:
			barWidth = 374 * (1 + 1/3);
			break;
		}
		coinInternal = 5 - EnhenceSys.coinInternalEnhence.get();
		disappearCount = 5 - EnhenceSys.disappearCountEnhence.get();
		removeAd = EnhenceSys.adEnhence.get();
		
//		sys.localStorage.setItem(COIN_COUNT_TAG, 200000);
		
		var startLayer;
//		if(REAL_WIN_SIZE.height/REAL_WIN_SIZE.width == 3/2)
//			startLayer = ccs.load(res.START_640X960_JSON).node;
//		else if(REAL_WIN_SIZE.height/REAL_WIN_SIZE.width == 16/9)
//			startLayer = ccs.load(res.Start_json).node;
//		else
		background = new cc.Sprite("res/bg.png");
		background.x = DESIGN_SIZE.width/2;
		background.y = DESIGN_SIZE.height/2;
		this.addChild(background);
		
		coinSprite = new cc.Sprite("res/CoinBg.png");
		coinSprite.x = 929.66;
		coinSprite.y = 1839.36;
		this.addChild(coinSprite);

		coinText = new ccui.Text("0", "res/DFWaWaW5-GB.ttc", 48, cc.size(125, 60), cc.TEXT_ALIGNMENT_LEFT, cc.VERTICAL_TEXT_ALIGNMENT_TOP);
		coinText.x = 888.52;
		coinText.y = 1839.94;
		coinText.setTextColor( cc.color( 255, 165, 0));
		this.addChild( coinText);

		coinText.setString(Storage.getCoin());
		
		sea1 = new cc.Sprite("res/hailang3.png");
		sea1.x = 540;
		sea1.y = 257.66;
		cc.log(sea1.width + ":" + sea1.height);
		sea1.scaleX = 0.7897;
		this.addChild(sea1);
		var act1 = cc.moveBy(2, cc.p( 0, 70));
		var reverse1 = cc.moveBy(2, cc.p( 0, -70));
		sea1.runAction(cc.repeatForever( cc.Sequence( act1, reverse1)));

		sea2 = new cc.Sprite("res/hailang 1.png");
		sea2.x = 540;
		sea2.y = 154.94;
		sea2.scaleX = 0.7897;
		this.addChild(sea2);
		var act2 = cc.moveBy(1.8, cc.p( 0, 50));
		var reverse2 = cc.moveBy(1.8, cc.p( 0, -50));
		sea2.runAction(cc.repeatForever( cc.Sequence( act2, reverse2)));

		sea3 = new cc.Sprite("res/hailang 2.png");
		sea3.x = 540;
		sea3.y = 45.31;
		sea3.scaleX = 0.7897;
		this.addChild(sea3);
		var act3 = cc.moveBy(1.7, cc.p( 0, 50));
		var reverse3 = cc.moveBy(1.7, cc.p( 0, -50));
		sea3.runAction(cc.repeatForever( cc.Sequence( act3, reverse3)));
		
		logo = new ccui.Button("res/logo.png");
		logo.x = 540;
		logo.y = 1492.99;
		this.addChild(logo);
		
		//乐借
		sun = new ccui.Button("res/Button_Sun.png");
		sun.x = 130;
		sun.y = 1300;
		this.addChild(sun);
		sun.name = "sun";
		sun.addClickEventListener(this.sunBtnClicked.bind(this));
		
		classicModeBtn = new ccui.Button("res/Button_ClassicModel.png");
		classicModeBtn.x = 540;
		classicModeBtn.y = 1127.23;
		this.addChild(classicModeBtn);
		classicModeBtn.addClickEventListener(this.classicModeBtnClicked.bind(this));
		
		propModeBtn = new ccui.Button("res/Button_PropsModel.png");
		propModeBtn.x = 540;
		propModeBtn.y = 883.2;
		this.addChild(propModeBtn);
		propModeBtn.addClickEventListener(this.propModeBtnClicked.bind(this));

		strengthenBtn = new ccui.Button("res/Button_Strengthen.png");
		strengthenBtn.x = 540;
		strengthenBtn.y = 631.1;
		this.addChild(strengthenBtn);
		strengthenBtn.addClickEventListener(this.strengthClicked.bind(this));
		
		musicBtn = new ccui.Button("res/Button_Closemusic.png");
		musicBtn.x = 251.62;
		musicBtn.y = 1816.69;
		this.addChild(musicBtn);
		musicBtn.addClickEventListener(this.musicClicked.bind(this));
		musicBtn.loadTextureDisabled( "res/Button_Upmusic.png", 0);

		if(!music)
			musicBtn.setBright(false);
		
		aboutBtn = new ccui.Button("res/Button_About.png");
		aboutBtn.x = 112.43;
		aboutBtn.y = 1816.69;
		this.addChild(aboutBtn);
		aboutBtn.addClickEventListener(this.aboutClicked.bind(this));

		if('touches' in cc.sys.capabilities) {   // 监听返回键，菜单键
			cc.eventManager.addListener({ 
				event: cc.EventListener.KEYBOARD, 
				onKeyReleased: function(keyCode, event) { 
					if (keyCode == cc.KEY.back) {   
						
						Storage.setMusicState(music);
						cc.audioEngine.stopMusic();
						cc.director.end(); 
					} 
					else if (keyCode == cc.KEY.menu) {    
						if(!storeShow){
							storeClick = true;
							storeShow = true;
						}
					} 
				}}, this);

			// 进入后台
			cc.eventManager.addCustomListener(cc.game.EVENT_HIDE, function(){ 
				MusicPlayer.pauseResumeMusic(false);
			});
			// 恢复显示
			cc.eventManager.addCustomListener(cc.game.EVENT_SHOW, function(){ 
				MusicPlayer.pauseResumeMusic(true);
			});
		}

		return true;
	},
	classicModeBtnClicked: function(sender) {
		
		MusicPlayer.playClickBtnEffect();
		
		cc.director.replaceScene(
				new cc.TransitionCrossFade(0.5, new ClassicModeSceneWithStudio()));
	},
	propModeBtnClicked: function(sender) {
		MusicPlayer.playClickBtnEffect();
		
		cc.director.replaceScene(
				new cc.TransitionCrossFade(0.5, new PropModeScene()));
	},
	sunBtnClicked: function(sender) {
		coin = parseInt(coinText.getString()) + 20;
		sys.localStorage.setItem(COIN_COUNT_TAG, coin);
		coinText.setString(coin);	

		jsb.reflection.callStaticMethod
			("org/cocos2dx/javascript/AppActivity", "invokeSun", "()V");
	},
	musicClicked: function(sender) {
		
		if(music){
			
			music= !music;
			sender.setBright(false);
			cc.audioEngine.pauseMusic();
		}else{

			music= !music;
			MusicPlayer.playClickBtnEffect();
			sender.setBright(true);
			MusicPlayer.playStartLayerBgMusic();
		}
	},
	aboutClicked: function(sender) {
		
		MusicPlayer.playClickBtnEffect();
		this.addChild(new AboutLayer(), 3);
	},
	strengthClicked: function(sender) {
		
		MusicPlayer.playClickBtnEffect();
		this.addChild(new StrengthLayer(), 3);
	}
});

var StartScene = cc.Scene.extend({
	onEnter:function () {
		
		this._super();
		this.addChild(new StartLayer());
	}
});

